<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 10/30/12
 * Time: 12:36 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Fe_Products_AjaxProducts extends Page {
	protected $productsFlashManager;
	function __construct() {
		header('Access-Control-Allow-Origin: *');
		$this->productsFlashManager = new ProductsFlashManager;
		parent::__construct();
	}

	public function defaultAction() {
		print 'Ajax Products';
	}

	/**
	 * @internal param $productId
	 * @internal param $isMemoryProductId
	 * @internal param $allTemplates
	 * @internal param $memoryIdentifier
	 * @return ApplicationConf|string
	 */
	public function getDataAction() {
		$productId = get('productId', 'str');
		$allTemplates = get('allTemplates', 'bool');
		$memoryIdentifier = get('memoryIdentifier', 'str');
		print json_encode($this->productsFlashManager->getData($productId, '', $memoryIdentifier, $allTemplates));
	}

	/**
	 * @internal param $sizeId
	 * @internal param $allTemplates
	 * @return array
	 */
	public function getTemplatesAction() {
		$sizeId = post('sizeId', 'id');
		$allTemplates = post('allTemplates', 'bool');
		print $this->productsFlashManager->getTemplates($sizeId, $allTemplates);
	}

	/**
	 * @internal param $sizeId
	 * @internal param $allTemplates
	 * @internal param int $productId
	 * @return array
	 */
	public function getFlashTemplatesAction() {
		$sizeId = post('sizeId', 'id');
		$allTemplates = post('allTemplates', 'bool');
		$productId = (post('productId', 'int') ? post('productId', 'int') : 0);
		print $this->productsFlashManager->getFlashTemplates($sizeId, $allTemplates, $productId);
	}

	public function getThemesAction() {
		print $this->productsFlashManager->getThemes();
	}

	/**
	 * @return array
	 */
	public function getDesignsAction() {
		$sizeId = post('sizeId', 'id');
		$productId = (post('productId', 'int') ? post('productId', 'int') : 0);
		print $this->productsFlashManager->getDesigns($sizeId, $productId);
	}

	public function uploadPhotoAction() {
		$dataUser = array(
			'type' => get('type', 'str'),
			'memoryIdentifier' => get('memoryIdentifier', 'str'),
			'ajax' => get('ajax', 'int')
		);
		$uploadedFileResult = $this->productsFlashManager->uploadPhoto($dataUser, $_FILES);
		echo "<script type=\"text/javascript\">
				parent.product.gallery_config.images[\"key_{$uploadedFileResult['ph_id']}\"] = " . json_encode($uploadedFileResult) .";
			 </script>";
	}


}
